package com.ghj.securityConfig;


import com.ghj.pojo.User;
import com.ghj.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;


public class UserRealm extends AuthorizingRealm {
   @Autowired
   UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken Token) throws AuthenticationException {
        System.out.println("在进行验证中===");
        ///获得当前用户，由login发送
        String name = (String) Token.getPrincipal();
        //到数据库查询
        User user= userService.findOneByName(name);
        if(user==null) {
           return null;
        }
        //密码认证，shiro做，将用户信息封装在AuthenticationInfo中，3个参数（）
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");

    }
}
